package extract;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

// 8485 sec: 1220341 operations; 158.58 current ops/sec; [POST AverageLatency(ms)=0.48] [GETHTL AverageLatency(ms)=193.01] 
public class LatencyExtractor {
	public static final String SEPERATOR = "\t";

	public static void main(String[] args) throws IOException {
		/*extract(new String[] { "data/hybrid_20121107131642", "data/hybrid_20121107131642_e.log",
				"data/hybrid_20121107131642_m.log" });*/
		
		extract(new String[] { "data/push_20121107124519", "data/push_20121107124519_e.log",
		"data/push_20121107124519_m.log" });
	}

	public static void extract(String[] args) throws IOException {
		DataInputStream dis = new DataInputStream(new FileInputStream(args[0]));
		DataOutputStream dos = new DataOutputStream(new FileOutputStream(
				args[1]));

		DataOutputStream mdos = new DataOutputStream(new FileOutputStream(
				args[2]));

		String line = null;
		try {
			int count = 1;
			int operations = 0;
			while (null != (line = dis.readLine())) {
				int startIndex = 0;
				int endIndex = line.indexOf("sec:");
				if (endIndex == -1) {
					if (isDigit(line)) {
						mdos.write(String.format("%d\t%s\n", operations, line)
								.getBytes());
					}
					continue;
				}
				int sec = Integer.parseInt(line.substring(startIndex, endIndex)
						.trim());

				startIndex = endIndex + "sec:".length();
				endIndex = line.indexOf("operations;", startIndex);
				if (endIndex == -1) {
					continue;
				}
				operations = Integer.parseInt(line.substring(startIndex,
						endIndex).trim());

				startIndex = endIndex + "operations;".length();
				endIndex = line.indexOf("current ops/sec;", startIndex);
				if (endIndex == -1) {
					continue;
				}
				float throughPut = Float.parseFloat(line.substring(startIndex,
						endIndex).trim());

				startIndex = endIndex + "current ops/sec;".length();
				startIndex = line.indexOf("(ms)=", startIndex)
						+ "(ms)=".length();
				endIndex = line.indexOf("]", startIndex);
				if (endIndex == -1) {
					continue;
				}
				float post = Float.parseFloat(line.substring(startIndex,
						endIndex).trim());

				startIndex = endIndex + "]".length();
				startIndex = line.indexOf("(ms)=", startIndex)
						+ "(ms)=".length();
				endIndex = line.indexOf("]", startIndex);
				if (endIndex == -1) {
					continue;
				}

				float htl = Float.parseFloat(line.substring(startIndex,
						endIndex).trim());
				String record = String.format("%d\t%d\t%d\t%f\t%f\t%f\n",
						count, sec, operations, throughPut, post, htl);
				System.out.print(record);
				dos.write(record.getBytes());
				count++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			dis.close();
			dos.close();
			mdos.close();
		}
	}

	public static Pattern pattern = Pattern.compile("[0-9]*");

	public static boolean isDigit(String value) {
		Matcher matcher = pattern.matcher(value);
		return matcher.matches();
	}

	@Test
	public void test() {
		System.out.println(isDigit("01231"));
		System.out.println(isDigit("01231 asdf "));
		System.out.println(isDigit("01231 123"));
	}
}
